#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QNetworkInterface>
#include <QClipboard>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), udpSocket(new QUdpSocket(this)), settings(new QSettings("YourCompany", "YourApp", this)) {
    setWindowTitle("UDP Receiver/Sender");

    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    // Local IP Display
    localIpLabel = new QLineEdit(getLocalIpAddress(), this);
    localIpLabel->setReadOnly(true);
    mainLayout->addWidget(localIpLabel);

    // Receive Text Area
    receiveTextEdit = new QTextEdit(this);
    receiveTextEdit->setReadOnly(true);
    mainLayout->addWidget(receiveTextEdit);

    // Cut Button
    cutButton = new QPushButton("Cut", this);
    connect(cutButton, &QPushButton::clicked, this, &MainWindow::cutText);
    mainLayout->addWidget(cutButton);

    // Send Text Area
    sendTextEdit = new QTextEdit(this);
    mainLayout->addWidget(sendTextEdit);

    // Paste Button and Send Button
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    pasteButton = new QPushButton("Paste", this);
    connect(pasteButton, &QPushButton::clicked, this, &MainWindow::pasteText);
    buttonLayout->addWidget(pasteButton);

    sendButton = new QPushButton("Send", this);
    connect(sendButton, &QPushButton::clicked, this, &MainWindow::sendDatagram);
    buttonLayout->addWidget(sendButton);

    mainLayout->addLayout(buttonLayout);

    // IP and Port Input
    QHBoxLayout *ipPortLayout = new QHBoxLayout;
    ipLineEdit = new QLineEdit(this);
    ipLineEdit->setPlaceholderText("IP Address");
    ipPortLayout->addWidget(ipLineEdit);

    portLineEdit = new QLineEdit(this);
    portLineEdit->setPlaceholderText("Port");
    ipPortLayout->addWidget(portLineEdit);

    mainLayout->addLayout(ipPortLayout);

    // Load settings
    loadSettings();

    // UDP Socket
    quint16 port = portLineEdit->text().toUInt();
    udpSocket->bind(QHostAddress::Any, port);
    connect(udpSocket, &QUdpSocket::readyRead, this, &MainWindow::readPendingDatagrams);
}

MainWindow::~MainWindow() {
    saveSettings();
}

void MainWindow::readPendingDatagrams() {
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size());
        receiveTextEdit->append(QString::fromUtf8(datagram));
    }
}

void MainWindow::sendDatagram() {
    QString ip = ipLineEdit->text();
    quint16 port = portLineEdit->text().toUInt();
    QByteArray datagram = sendTextEdit->toPlainText().toUtf8();
    udpSocket->writeDatagram(datagram, QHostAddress(ip), port);
    sendTextEdit->clear();
}

void MainWindow::cutText() {
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setText(receiveTextEdit->toPlainText());
    receiveTextEdit->clear();
}

void MainWindow::pasteText() {
    QClipboard *clipboard = QApplication::clipboard();
    sendTextEdit->setText(clipboard->text());
}

QString MainWindow::getLocalIpAddress() {
    const QList<QHostAddress> &addresses = QNetworkInterface::allAddresses();
    for (const QHostAddress &address : addresses) {
        if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress::LocalHost) {
            return address.toString();
        }
    }
    return QHostAddress(QHostAddress::LocalHost).toString();
}

void MainWindow::loadSettings() {
    ipLineEdit->setText(settings->value("ip", "192.168.0.114").toString());
    portLineEdit->setText(settings->value("port", "45454").toString());
}

void MainWindow::saveSettings() {
    settings->setValue("ip", ipLineEdit->text());
    settings->setValue("port", portLineEdit->text());
}
